<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>系统安装 - MultiLang Admin</title>
    <script src="/static/jquery-3.5.1/jquery-3.5.1.min.js"></script>
    <link href="/static/bootstrap-5.3.0-alpha1-dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="/static/bootstrap-5.3.0-alpha1-dist/js/bootstrap.bundle.min.js"></script>

    <style>
        :root {
            --primary-color: #2563eb;
            --primary-light: #3b82f6;
            --secondary-color: #64748b;
            --bg-color: #f8fafc;
            --card-bg: #ffffff;
            --heading-color: #1e293b;
            --text-color: #64748b;
            --border-radius: 16px;
            --shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.08);
        }

        body {
            background-color: var(--bg-color);
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
        }

        .container {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
            padding: 2rem 1rem;
        }

        .card {
            border: none;
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
            background: linear-gradient(to bottom, #ffffff, #f8fafc);
        }

        .card-header {
            background: transparent;
            border-bottom: 1px solid #eee;
            padding: 2rem;
            border-radius: var(--border-radius) var(--border-radius) 0 0 !important;
        }

        .card-body {
            padding: 2rem;
        }

        .logo-container {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            margin-bottom: 0;
        }

        .logo {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            box-shadow: 0 8px 16px rgba(37, 99, 235, 0.15);
        }

        .logo-text {
            font-size: 24px;
            font-weight: 700;
            color: var(--heading-color);
        }

        .logo-text span {
            color: var(--primary-color);
        }

        .logo-subtitle {
            font-size: 14px;
            color: var(--text-color);
        }

        .progress {
            height: 3px;
            margin: 0 0 2rem 0;
            background-color: #e2e8f0;
            border-radius: 0;
        }

        .progress-bar {
            background-color: var(--primary-color);
        }

        .step-box {
            display: none;
            padding: 1.5rem;
            opacity: 0;
            transform: translateY(20px);
            transition: all 0.3s ease-out;
        }

        .step-box.active {
            display: block;
            opacity: 1;
            transform: translateY(0);
        }

        .step-title {
            color: var(--heading-color);
            font-size: 1.25rem;
            font-weight: 600;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #eee;
        }

        .table {
            margin-bottom: 2rem;
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }

        .table td {
            padding: 1rem 1.5rem;
            vertical-align: middle;
        }

        .table tr:last-child td {
            border-bottom: none;
        }

        .form-control {
            border-radius: 10px;
            border-color: #e2e8f0;
            padding: 0.875rem 1.25rem;
            background-color: #f8fafc;
        }

        .form-label {
            margin-bottom: 0.75rem;
            font-size: 0.9rem;
            color: var(--heading-color);
            font-weight: 500;
        }

        .form-text {
            margin-top: 0.5rem;
            font-size: 0.85rem;
        }

        .btn-group {
            display: flex;
            justify-content: flex-end;
            gap: 0.75rem;
            margin-top: 2rem;
        }

        .btn {
            padding: 0.5rem 1.25rem;
            border-radius: 8px;
            font-weight: 400;
            font-size: 0.9rem;
            min-width: 100px;
        }

        .btn:active {
            transform: scale(0.98);
        }

        .btn-primary {
            background-color: var(--primary-color);
            border: none;
            min-width: 80px;
        }

        .btn-primary:hover {
            background-color: var(--primary-light);
        }

        .btn-secondary {
            background-color: #e2e8f0;
            border: none;
            color: var(--text-color);
            min-width: 80px;
        }

        .btn-secondary:hover {
            background-color: #cbd5e1;
            color: var(--heading-color);
        }

        .alert {
            border-radius: 8px;
            border: none;
            animation: slideIn 0.3s ease-out;
            padding: 2rem;
            text-align: center;
            margin: 2rem 0;
            background-color: #fef2f2;
            color: #dc2626;
            box-shadow: 0 4px 6px -1px rgba(220, 38, 38, 0.1);
        }

        .error-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 2rem 1rem;
        }

        .error-icon {
            font-size: 3rem;
            color: #dc2626;
            margin-bottom: 1.5rem;
        }

        .error-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #dc2626;
            margin-bottom: 1rem;
        }

        .error-message {
            color: #ef4444;
            font-size: 1rem;
            max-width: 400px;
            line-height: 1.6;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @media (max-width: 768px) {
            .container {
                padding: 1rem;
            }
            
            .card-body {
                padding: 1rem;
            }
        }

        /* 进度条动画 */
        .progress-bar {
            background-color: var(--primary-color);
            transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
        }

        /* 按钮悬停动画 */
        .btn {
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.2s ease;
        }

        .btn:active {
            transform: translateY(1px);
        }

        /* 表单控件动画 */
        .form-control {
            border-radius: 8px;
            border-color: #e2e8f0;
            padding: 0.75rem 1rem;
            transition: all 0.2s ease;
        }

        .form-control:focus {
            border-color: var(--primary-light);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
            transform: translateY(-1px);
        }

        /* 卡片悬停效果 */
        .card {
            border: none;
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
            transition: box-shadow 0.3s ease;
        }

        .card:hover {
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }

        /* 错误提示动画 */
        .alert {
            border-radius: 8px;
            border: none;
            animation: slideIn 0.3s ease-out;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 表格行悬停效果 */
        .table tr {
            transition: background-color 0.2s ease;
        }

        .table tr:hover {
            background-color: rgba(37, 99, 235, 0.05);
        }

        /* Logo 容器动画 */
        .logo-container {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            animation: fadeIn 0.6s ease-out;
        }

        .logo {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(13, 110, 253, 0.2);
            transition: transform 0.3s ease;
        }

        .logo:hover {
            transform: rotate(5deg);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 加载动画 */
        .loading {
            display: inline-block;
            width: 1.2rem;
            height: 1.2rem;
            border: 2px solid rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            border-top-color: var(--primary-color);
            animation: spin 1s linear infinite;
            vertical-align: -0.2em;
        }

        /* 按钮加载状态 */
        .btn .loading {
            margin-right: 0.5rem;
            margin-left: -0.25rem;
        }

        /* 调整按钮间距 */
        .btn + .btn {
            margin-left: 0.5rem;
        }

        /* 调整主要按钮的大小 */
        .btn-primary {
            background-color: var(--primary-color);
            border: none;
            min-width: 80px;
        }

        /* 调整次要按钮的大小 */
        .btn-secondary {
            background-color: #e2e8f0;
            border: none;
            color: var(--text-color);
            min-width: 80px;
        }

        /* 调整信息按钮的大小 */
        .btn-info {
            min-width: 90px;
            position: relative;
            background-color: var(--primary-light);
            border: none;
            color: white;
        }

        .btn-info:hover {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-info:active,
        .btn-info:focus {
            background-color: var(--primary-color) !important;
            color: white !important;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .btn-info:disabled {
            background-color: var(--primary-light);
            opacity: 0.7;
            color: white;
        }

        .complete-container {
            text-align: center;
            padding: 3rem 2rem;
        }

        .complete-icon {
            font-size: 3rem;
            color: #22c55e;
            margin-bottom: 1.5rem;
        }

        .complete-title {
            font-size: 1.5rem;
            color: var(--heading-color);
            margin-bottom: 1rem;
        }
    </style>
</head>

<body>
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <div class="logo-container">
                            <img src="/static/images/logo.svg" alt="Logo" class="logo">
                            <div>
                                <div class="logo-text">MultiLang <span>Admin</span></div>
                                <div class="logo-subtitle">系统安装向导</div>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <!-- 错误信息占位符 -->
                        <!--ERROR_MESSAGE-->
                        
                        <!-- 进度条 -->
                        <div class="progress">
                            <div class="progress-bar" role="progressbar" style="width: 0%"></div>
                        </div>

                        <!-- 步骤1: 环境检测 -->
                        <div class="step-box active" id="step1">
                            <h5 class="step-title">环境检测</h5>
                            <div id="env-check" class="mb-4"></div>
                            <div class="text-end">
                                <button class="btn btn-primary px-4" onclick="nextStep(2)">下一步</button>
                            </div>
                        </div>

                        <!-- 步骤2: 数据库配置 -->
                        <div class="step-box" id="step2">
                            <h5 class="step-title">数据库配置</h5>
                            <form id="dbForm">
                                <div class="mb-3">
                                    <label class="form-label">数据库类型</label>
                                    <select class="form-control" name="db_type">
                                        <option value="mysql">MySQL</option>
                                        <option value="pgsql" disabled>PostgreSQL (暂不支持)</option>
                                        <option value="sqlite" disabled>SQLite (暂不支持)</option>
                                        <option value="sqlsrv" disabled>SQL Server (暂不支持)</option>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据库主机</label>
                                    <input type="text" class="form-control" name="db_hostname" value="127.0.0.1">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据库端口</label>
                                    <input type="text" class="form-control" name="db_hostport" value="3306">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据库名称</label>
                                    <input type="text" class="form-control" name="db_database" value="multilang">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据库用户名</label>
                                    <input type="text" class="form-control" name="db_username" value="root">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据库密码</label>
                                    <input type="password" class="form-control" name="db_password">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">数据表前缀</label>
                                    <input type="text" class="form-control" name="db_prefix" value="ml_">
                                    <div class="form-text">建议使用默认前缀，如果需要修改请同时修改SQL文件中的表前缀</div>
                                </div>
                                <div class="btn-group">
                                     <button type="button" class="btn btn-secondary" onclick="prevStep(1)">上一步</button>
                                     <button type="button" class="btn btn-info" onclick="testDb()">测试连接</button>
                                     <button type="button" class="btn btn-primary" onclick="nextStep(3)">下一步</button>
                                 </div>
                            </form>
                        </div>

                        <!-- 步骤3: 管理员配置 -->
                        <div class="step-box" id="step3">
                            <h5 class="step-title">管理员配置</h5>
                            <form id="adminForm">
                                <div class="mb-3">
                                    <label class="form-label">管理员账号</label>
                                    <input type="text" class="form-control" name="admin_username" value="admin">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">管理员密码</label>
                                    <input type="password" class="form-control" name="admin_password">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">确认密码</label>
                                    <input type="password" class="form-control" name="admin_password_confirm">
                                </div>
                                <div class="text-end">
                                    <button type="button" class="btn btn-secondary" onclick="prevStep(2)">上一步</button>
                                    <button type="button" class="btn btn-primary" onclick="install()">开始安装</button>
                                </div>
                            </form>
                        </div>

                        <!-- 步骤4: 安装完成 -->
                        <div class="step-box" id="step4">
                            <div class="complete-container">
                                <i class="fa fa-check-circle complete-icon"></i>
                                <h4 class="complete-title">安装完成！</h4>
                                <p class="text-muted">系统安装成功，请删除 install 目录并修改配置文件权限</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 当前步骤
        let currentStep = 1;

        // 页面加载完成后先检查安装状态
        $(document).ready(function () {
            // 检查是否已安装
            $.ajax({
                url: '/install/index',
                type: 'GET',
                success: function(res, status, xhr) {
                    // 获取错误信息响应头
                    let error = xhr.getResponseHeader('X-Error');
                    if(error) {
                        // 有错误信息则显示错误页面
                        // 隐藏所有步骤
                        $('.step-box').hide();
                        // 隐藏进度条
                        $('.progress').hide();
                        // 显示错误信息
                        $('.card-body').prepend(`
                            <div class="error-container">
                                <i class="fa fa-exclamation-circle error-icon"></i>
                                <h4 class="error-title">安装失败</h4>
                                <p class="error-message">${decodeURIComponent(error)}</p>
                            </div>
                        `);
                    } else {
                        // 无错误则继续检查环境
                        checkEnv();
                    }
                },
                error: function(xhr) {
                    console.error('请求失败:', xhr);
                }
            });
        });

        // 检查环境
        function checkEnv() {
            $.get('/install/checkenv', function (res) {
                if (res.code === 200) {
                    let html = '<table class="table">';
                    // PHP版本检查
                    html += `<tr>
                        <td>PHP版本</td>
                        <td>${res.data.php.current}</td>
                        <td>${res.data.php.required}</td>
                        <td>${res.data.php.result ? '✅' : '❌'}</td>
                    </tr>`;
                    // 扩展检查
                    res.data.extensions.forEach(ext => {
                        html += `<tr>
                            <td>${ext.name}</td>
                            <td>${ext.current}</td>
                            <td>${ext.required}</td>
                            <td>${ext.result ? '✅' : '❌'}</td>
                        </tr>`;
                    });
                    // 目录检查
                    res.data.dirs.forEach(dir => {
                        html += `<tr>
                            <td>${dir.name}</td>
                            <td>${dir.current}</td>
                            <td>${dir.required}</td>
                            <td>${dir.result ? '✅' : '❌'}</td>
                        </tr>`;
                    });
                    html += '</table>';
                    $('#env-check').html(html);
                }
            });
        }

        // 测试数据库连接
        function testDb() {
            let $btn = $('.btn-info');
            let originalText = $btn.html();
            $btn.css('color', '#ffffff');
            $btn.html('<span class="loading me-2"></span>测试中...');
            $btn.prop('disabled', true);

            let data = $('#dbForm').serialize();
            $.post('/install/checkdb', data, function (res) {
                setTimeout(() => {
                    $btn.html(originalText);
                    $btn.prop('disabled', false);
                    alert(res.code === 200 ? '连接成功' : res.msg);
                }, 500);
            });
        }

        // 安装系统
        function install() {
            // 验证密码
            let pwd = $('input[name="admin_password"]').val();
            let pwdConfirm = $('input[name="admin_password_confirm"]').val();
            if (pwd !== pwdConfirm) {
                alert('两次输入的密码不一致');
                return;
            }

            let $btn = $('.btn-primary:last');
            let originalText = $btn.html();
            $btn.css('color', '#ffffff');
            $btn.html('<span class="loading me-2"></span>安装中...');
            $btn.prop('disabled', true);

            // 获取数据库配置
            let dbData = $('#dbForm').serialize();
            // 获取管理员配置
            let adminData = $('#adminForm').serialize();

            // 开始安装
            $.post('/install/createdb', dbData)
                .then(() => $.post('/install/importdb', dbData))
                .then(() => $.post('/install/createadmin', adminData))
                .then(() => $.post('/install/complete'))
                .then(() => {
                    $btn.html(originalText);
                    $btn.prop('disabled', false);
                    nextStep(4);
                })
                .catch(err => {
                    $btn.html(originalText);
                    $btn.prop('disabled', false);
                    alert('安装失败：' + err.responseJSON.msg);
                });
        }

        // 下一步
        function nextStep(step) {
            $('.step-box.active').fadeOut(200, function() {
                $(this).removeClass('active');
                $('#step' + step).fadeIn(200).addClass('active');
                currentStep = step;
                updateProgress();
            });
        }

        // 上一步
        function prevStep(step) {
            $('.step-box.active').fadeOut(200, function() {
                $(this).removeClass('active');
                $('#step' + step).fadeIn(200).addClass('active');
                currentStep = step;
                updateProgress();
            });
        }

        // 更新进度条
        function updateProgress() {
            let percent = (currentStep - 1) * 33.33;
            if (currentStep === 4) percent = 100;
            $('.progress-bar').css('width', percent + '%');
        }
    </script>
</body>
</html>